<template>
    <div class="main-container">
        <div class="header">
            <div class="title">Trading</div>
        </div>
        <div class="content">
            <div class="left-box">
                <div class="sub-title">Stock Search</div>
                <div class="search-box">
                    <div class="icon" style="--w: 1.2vw; --h: 1.2vw; --icon: url(/icon/search1.svg)"></div>
                    <input type="text" v-model="search_stock" placeholder="Search stocks ..." />
                </div>
                <div class="stock-list">
                    <div class="stock-item" v-for="item in show_stock" :key="item.id" @click="active_stock = item" :style="`--bgc: ${ item.id == active_stock?.id ? '#d5d5d5' : '#00000000' }`">
                        <div class="info-box">
                            <div class="symbol">{{ item.symbol }}</div>
                            <div class="name">{{ item.name }}</div>
                        </div>
                        <div class="price-box">
                            <div class="value">{{ item.price }}</div>
                            <div class="mover" :style="`color: ${ parseFloat(item.change) >= 0 ? 'var(--positive-number-color)' : 'var(--negative-number-color)' }`">{{ item.change }}</div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="right-box">
                <div class="stock-info">
                    <div class="title">{{ active_stock?.symbol }}</div>
                    <div class="more">
                        <div class="value">{{ active_stock?.price }}</div>
                        <div class="mover" :style="`color: ${ parseFloat(active_stock?.change) >= 0 ? 'var(--positive-number-color)' : 'var(--negative-number-color)' }`">{{ active_stock?.change }}</div>
                    </div>
                </div>
                <div class="tabbar">
                    <div class="tabbar-item" v-for="item in tabbar" :key="item.id" :style="`background-color: ${ item.id == active_tabbar.id ? '#ccc' : '#00000000' }`" @click="active_tabbar = item">{{ item.text }}</div>
                </div>
                <div class="form-box">
                    <div class="input-name">Type</div>
                    <div class="input-line">
                        <select name="orderType" id="orderType" v-model="active_trading_type">
                            <option :value="item.value" v-for="item in trading_type_list" :key="item.id">{{ item.text }}</option>
                        </select>
                    </div>
                    <div class="input-name">Quantity</div>
                    <div class="input-line">
                        <input type="number" min="1" v-model="quantity">
                    </div>
                    <div class="input-name" v-show="active_trading_type == 1">Limit Price</div>
                    <div class="input-line" v-show="active_trading_type == 1">
                        <input type="number" min="1" v-model="limit_price" step="0.01">
                    </div>
                    <div class="price-per-share-box">
                        <div class="price-box">
                            <div class="price-name">Price per share</div>
                            <div class="price">{{ active_stock?.price }}</div>
                        </div>
                        <div class="count-box">
                            <div class="quantity">Quantity</div>
                            <div class="count">{{ quantity }}</div>
                        </div>
                        <div class="total-cost">
                            <div class="total-text">Total Cost</div>
                            <div class="total-price">{{ total_price }}</div>
                        </div>
                        <div class="btn-box" :style="`background-color: ${ active_tabbar.id == 1 ? 'var(--positive-number-color)' : 'var(--negative-number-color)' }`" @click="stockOpen">
                            <div class="btn">{{ active_tabbar.text }} {{ active_stock?.symbol }}</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { active_stock, search_stock, show_stock, tabbar, active_tabbar, trading_type_list, active_trading_type, quantity, total_price, limit_price, tradingService } from '@/service/trade.js';
import { stock_list } from '@/service/market.js';
import { onMounted } from 'vue';
import { loading } from '@/service/status';

async function stockOpen() {
    loading.value = true;
    await tradingService.stockOpen();
    loading.value = false;
}

onMounted(() => {
    if (!active_stock.value) active_stock.value = stock_list.value[0];
});
</script>

<style scoped lang="less">
.main-container {
    box-sizing: border-box;
    width: 100%;
    min-height: calc(100vh - var(--nav-height) - var(--footer-height));
    padding: 1.5vw 11vw;
    display: flex;
    flex-direction: column;
    gap: 2vw;

    .header {
        display: flex;
        justify-content: space-between;
        align-items: center;

        .title {
            font-size: 1.8vw;
            font-weight: 600;
            color: var(--text-color);
        }
    }

    .content {
        width: 100%;
        height: 35vw;
        display: flex;
        gap: 1vw;
        
        .left-box, .right-box {
            flex: 1;
            background-color: #fff;
            box-shadow: 0 0 0.5vw rgba(0, 0, 0, 0.05);
            border-radius: 0.5vw;
            padding: 1vw;
            display: flex;
            flex-direction: column;
            gap: 1vw;
        }

        .left-box {
            display: flex;
            flex-direction: column;

            .sub-title {
                font-size: 1.2vw;
                font-weight: 600;
                color: var(--text-color);
            }

            .search-box {
                box-sizing: border-box;
                width: 100%;
                height: 3vw;
                padding: 0.5vw;
                display: flex;
                align-items: center;
                justify-content: flex-start;
                gap: 0.5vw;
                background-color: var(--bg-strong-color);
                border-radius: 0.5vw;
    
                input {
                    width: 100%;
                    background-color: #ffffff00;
                    border: 0;
                    outline: 0;
                    font-size: 0.8vw;
                }
            }

            .stock-list {
                flex: 1;
                display: flex;
                flex-direction: column;
                gap: 0.5vw;
                overflow-y: auto;

                .stock-item {
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    padding: 0.5vw;
                    border-radius: 0.5vw;
                    background-color: var(--bgc);

                    &:hover {
                        cursor: pointer;
                        background-color: #eee;
                    }

                    .info-box {
                        display: flex;
                        flex-direction: column;
                        gap: 0.2vw;

                        .symbol {
                            font-size: 1vw;
                            font-weight: 600;
                            color: var(--text-color);
                        }

                        .name {
                            font-size: 0.8vw;
                            color: var(--text-light-color);
                        }
                    }

                    .price-box {
                        display: flex;
                        flex-direction: column;
                        align-items: flex-end;
                        gap: 0.2vw;

                        .value {
                            font-size: 0.8vw;
                            font-weight: 600;
                            color: var(--text-color);
                        }

                        .mover {
                            font-size: 0.7vw;
                        }
                    }
                }
            }
        }

        .right-box {
            .stock-info {
                display: flex;
                justify-content: space-between;
                align-items: center;

                .title {
                    font-size: 1.5vw;
                    font-weight: 600;
                    color: var(--text-color);
                }

                .more {
                    display: flex;
                    flex-direction: column;
                    align-items: flex-end;

                    .value {
                        font-size: 1vw;
                        font-weight: 600;
                        color: var(--text-color);
                    }

                    .mover {
                        font-size: 0.7vw;
                        color: var(--positive-number-color);
                    }
                }
            }

            .tabbar {
                display: flex;
                justify-content: space-between;
                align-items: center;
                background-color: #f5f5f5;
                padding: 0.3vw;
                border-radius: 0.5vw;

                .tabbar-item {
                    flex: 1;
                    text-align: center;
                    border-radius: 0.5vw;
                    padding: 0.2vw;
                }
            }

            .form-box {
                display: flex;
                flex-direction: column;
                gap: 1vw;

                .input-name {
                    font-size: 0.9vw;
                    color: var(--text-color);
                }

                .input-line {
                    padding: 0.3vw;
                    border-radius: 0.3vw;
                    background-color: var(--bg-strong-color);

                    input {
                        box-sizing: border-box;
                        width: 100%;
                        padding-left: 0.5vw;
                        background-color: #00000000;
                        border: 0;
                        outline: 0;
                    }

                    select {
                        width: 100%;
                        height: 100%;
                        background-color: #ffffff00;
                        border: 0;
                        outline: 0;
                        display: flex;

                        .select-item {
                            flex: 1;
                            text-align: center;
                            border-radius: 0.5vw;
                            padding: 0.2vw;
                        }
                    }
                }

                .price-per-share-box {
                    display: flex;
                    flex-direction: column;
                    gap: 0.5vw;

                    .price-box {
                        display: flex;
                        justify-content: space-between;

                        &>div {
                            font-size: 0.8vw;
                        }
                    }

                    .count-box {
                        display: flex;
                        justify-content: space-between;

                        &>div {
                            font-size: 0.8vw;
                        }
                    }

                    .total-cost {
                        display: flex;
                        justify-content: space-between;
                        border-top: 0.1vw solid var(--border-color);
                        margin-top: 0.5vw;
                        padding-top: 0.5vw;

                        &>div {
                            font-size: 0.9vw;
                        }
                    }

                    .btn-box {
                        margin-top: 2vw;
                        width: 100%;
                        height: 2.5vw;
                        border-radius: 0.5vw;
                        display: flex;
                        justify-content: center;
                        align-items: center;
                        position: relative;

                        &::after {
                            content: '';
                            display: block;
                            width: 100%;
                            height: 100%;
                            border-radius: 0.5vw;
                            position: absolute;
                            top: 0;
                            left: 0;
                        }

                        &:hover::after {
                            background-color: #00000020;
                        }

                        .btn {
                            font-size: 0.8vw;
                            color: #fff;
                        }
                    }
                }
            }
        }
    }
}
</style>